var resultdiv = "bodyDiv";
var infodiv = "progressdiv"
var infodivtext = "progressdivtext";

function getXmlHttp() {
	/* Create a new XMLHttpRequest for IE & Mozilla */
	var pxmlHttp = false;
	
	// IE Section
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
		xmlHttp = false;
	  }
	}
	@end @*/
	
	// Mozilla Section
	if (!pxmlHttp && typeof XMLHttpRequest != 'undefined') {
		try {
		  pxmlHttp = new XMLHttpRequest();
		} catch (e) {
			
		}
	}	
	
	if (!pxmlHttp)
		alert("Oops, sorry, something is not working, please try again :)");
	
	return pxmlHttp;
}
/* ---------------------------------------------- */

var xmlHttp = false;

/* Send a Request to the Server */
function sendRequest(url , params, bool) {

	xmlHttp = getXmlHttp();
	
	var fullurl = url;
	//var num = Math.floor(Math.random()*1024) + Math.floor(Math.random()*1024);
	//var ms = new Date().getTime() + num;
    var ms = "&requestid=" + Math.random(); // This is to avoid cache of GET request in IE
    
	if (!(params == null)) {
		fullurl = url + "?" + params + ms;	  
	} else {
	  	fullurl = url + ms;
	}

	var async = true;
	if (bool!=null)
		async = bool;
	
	// Build a Connection to the Server (true minds asynchronoues connection, set to false make browser wait for a response)		
	xmlHttp.open("POST", fullurl, async);

  // Setup a function for the server to run when it's done
  xmlHttp.onreadystatechange = processResponse;	

	// Send the request (send is set to null cause the params has been added to the url)
  startProcess();
  
  xmlHttp.send(null);

}

// HERE WE SET THE CONTENT INTO THE DIV
function processResponse() {

	if (xmlHttp.readyState == 4) {
		endProcess();
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			document.getElementById(resultdiv).innerHTML = response;
		} else {
			alert("Oops, we have a problem (ERR " + xmlHttp.status + ")");
		}
	}

}

function startProcess(text) {
	var obj = document.getElementById(infodiv);
	var objt = document.getElementById(infodivtext);
	if (obj!=null) {
		if (objt!=null) {
			if (text!=null) {
			  objt.innerHTML = text;
			} else {
			  objt.innerHTML = "Aguarde ...";
			}
		}
		obj.style.display = "";
		//showProgressDiv(infodiv);
		hideDivs("hideme");
	}
}

function endProcess() {
	var obj = document.getElementById(infodiv);
	if (obj!=null) {
		obj.style.display = "none";
	}
	hideDivs("hideme",false);
}
